$(function() {
    Base.init();
});

var Base = {
    oTimer : '',
    iTimerOn : 0,
    spinner : '',
        
    init: function()
    {
    },
    
    trim: function(str)
    {
        var str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length; while (ws.test(str.charAt(--i))); return str.slice(0, i + 1);
    },
    
    /**
     * 메시지 레이어 박스 출력
     */
    messageIn: function(sMsg, textMode, iWidth, bOut)
    {
        if (sMsg == undefined) var sMsg = '메세지가 없습니다.';
        if (textMode == undefined) var textMode = 'msg';
        if (bOut == undefined) var bOut = true;;
        if (iWidth == undefined) var iWidth = 600;
        
        if ($('#box_message').attr('id') != undefined) {
            $('#box_message').remove();
            if (this.iTimerOn == 1) {
                clearTimeout(this.oTimer);
                this.iTimerOn = 0;
            }
        }
        
        var textStyle = '';
        if (textMode == 'error') {
            textStyle = 'color:red;';
        } else if (textMode == 'ok') {
            textStyle = 'color:green;';
        }
        
        var sHtml = '<div id="box_message" class="box2" style="display:none; float:right; margin-top:10px; width:'+iWidth+'px; '+textStyle+'">'+sMsg+'</div>';
        $('#container').prepend(sHtml);
        $('#box_message').css({'float':'right'}).fadeIn('slow');
        
        if (bOut) {
            this.oTimer = setTimeout('Base.messageOut();', 3000);
            this.iTimerOn = 1;
        }
    },
    
    /**
     * 메시지 레이어 박스 제거
     */
    messageOut: function()
    {
        if ($('#box_message').attr('id') != undefined) {
            $('#box_message').fadeOut('slow');
            this.iTimerOn = 0;
        }
    },
    
    showBg: function()
    {
        var w = $(window).width();
        var h = $(window).height();
        
        $('body').append('<div id="el_bg" style="position:absolute; top:0; left:0; background:#EBEBEB; opacity:0.4; filter:alpha(opacity=40); width:'+w+'px; height:'+h+'px;"></div>');
    },
    
    hideBg: function()
    {
        if ($('#el_bg').attr('id') == 'el_bg')  {
            $('#el_bg').remove();
        }
    },
    
    /**
     * 로딩 시작
     */
    showSpin: function()
    {
        Base.showBg();
        
        $('body').append('<div id="el_spinner" style="position:absolute; top:50px; left:50px;"></div>');
        
        var opts = {
            lines: 14, // The number of lines to draw
            length: 11, // The length of each line
            width: 6, // The line thickness
            radius: 19, // The radius of the inner circle
            color: '#000', // #rgb or #rrggbb
            speed: 1, // Rounds per second
            trail: 56, // Afterglow percentage
            shadow: true // Whether to render a shadow
        };

        var target = document.getElementById('el_spinner');
        this.spinner = new Spinner(opts).spin(target);
    },
    
    /**
     * 로딩 멈춤
     */
    hideSpin: function()
    {
        if ($('#el_spinner').attr('id') == 'el_spinner')  {
            //$('#el_spinner').remove();
            Base.hideBg();
            Base.spinner.stop();
        }
    },
    
    showPlaceHolder: function(objId, sText)
    {
        if ($('#'+objId).attr('id') == objId) {
            $('#'+objId).attr('value', sText);
        }
    },
    
    hidePlaceHolder: function(objId)
    {
        if ($('#'+objId).attr('id') == objId) {
            $('#'+objId).attr('value', '');
        }
    },
    
    /**
     * 로그인
     */
    login: function()
    {
        var aValid = [
              ['아이디', 'email', 'login_id', 0, 80]
              , ['비밀번호', 'eng_all_num', 'login_pass', 6, 20]
          ];
        
        var _check = FormValidation.checked(aValid);
        if (!_check) {
            return false;
        }
        
        var params = 'm=setLogin&'+$('#loginForm').serialize();

        $.ajax({
            type: 'POST',
            url: '/',
            dataType: 'json',
            data: params,
            success:function(data) {
                if (data.code == 200) {
                    //alert('정상적으로 처리되었습니다.');
                    window.location.href = '/';
                    
                } else {
                    alert('아이디가 없거나 비밀번호가 잘못 되었습니다. [ code : '+data.code+']');
                }
            }
        });
    }
};